/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ProcessServlet;

import entity.bean.PurchaseRec;
import entity.bean.Show;
import entity.bean.Ticket;
import entity.connector.PurchaseUploader;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author 52571783
 */
public class Payment extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try {
            /*
             * TODO output your page here. You may use following sample code.
             */
            //declaration of the local variable
            //variable for webpage display information
            HttpSession session = request.getSession();
            PurchaseUploader purchaseUploader;
            Ticket[] orderedTicket;
            Integer noOfSeatMarked;
            String partialPayment;
            String loyaltyPt;
            Integer amount;
            String memberPw;
            String paymentMethod;
            String cardNumber;
            String cardType;
            PurchaseRec purchaseRec;
            Show selectedTimeSection;
            
            //the forwarding url
            String forwardURL;

            //getting parameter from request
            partialPayment = request.getParameter("partialPayment");
            paymentMethod = request.getParameter("paymentMethod");
            cardNumber = request.getParameter("cardNumber");
            cardType = request.getParameter("cardType");
            memberPw = request.getParameter("memberPw");
            loyaltyPt = request.getParameter("loyaltyPt");

            //end of getting  parameter



            //getting attribute from session
            selectedTimeSection = (Show) session.getAttribute("selectedTimeSection");
            purchaseRec = (PurchaseRec) session.getAttribute("purchaseRec");
            purchaseUploader = (PurchaseUploader) session.getAttribute("purchaseUploader");
            noOfSeatMarked = (Integer) session.getAttribute("noOfSeatMarked");
            orderedTicket = (Ticket[]) session.getAttribute("orderedTicket");
            //initialize the variable
            forwardURL = "/confirmation.jsp";

            for (int i = 1; i <= noOfSeatMarked; i++) {
                if (request.getParameter("ticketType" + i).equals("discount")) {
                    orderedTicket[i - 1].setDiscount(true);
                } else {
                    orderedTicket[i - 1].setDiscount(false);
                }
            }
            //confirm the type of tickets
            for (int i = 0; i < noOfSeatMarked; i++) {
                purchaseUploader.confirmTicket(orderedTicket[i]);
            }
            //

            amount = 0;
            //end of initialization 


            //computation of the total amount
            for (int i = 0; i < noOfSeatMarked; i++) {
                if (orderedTicket[i].getDiscount()) {
                    amount += (selectedTimeSection.getPrice()-15);
                } else {
                    amount += selectedTimeSection.getPrice();
                }
            }
            //end of computation amount

            //computation for loyalty point
            if (paymentMethod.equals("point")) {
                loyaltyPt = amount.toString();
            }



            //prepare the session
            session.setAttribute("loyaltyPt", loyaltyPt);
            session.setAttribute("amount", amount);
            session.setAttribute("partialPayment", partialPayment);
            session.setAttribute("cardNumber", cardNumber);
            session.setAttribute("paymentMethod", paymentMethod);
            session.setAttribute("cardType", cardType);
            session.setAttribute("noOfSeatMarked", noOfSeatMarked);
            session.setAttribute("memberPw", memberPw);
            //setting attribute for the beans


            //end of setting



            //forwarding the url of jsp
            RequestDispatcher dispatcher =
                    getServletContext().getRequestDispatcher(forwardURL);
            dispatcher.forward(request, response);
        } finally {
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
